<?php 
/**
 * ... ...
 * ... ...
 *
 */ 

/**
 * 视图类 JJ_HistoryDialog 
 *
 * @access public
 */
class JJ_HistoryDialog 	extends JCAT_ViewGtk implements JCAT_ISingleton 
{
	
	/**
	 * 构造函数 
	 *
	 * @access	public
	 * @param	$aParent	JCAT_IViewContainer		视图的父对象(父视图 或 控制器)。注意：视图不应单独存在，必须有一个父对象！ 
	 * @return	void
	 */
	public function JJ_HistoryDialog(JJ_MainController $aParent, JJ_Account $aAccount)
	{
		// 调用父类构造函数 
		//<<[UnmodifiableBlock.Start]::Call Parent Construct::>>//
		$this->JCAT_ViewGtk('JJ_HistoryDialog','template.JJ_HistoryDialog.glade','JJ_HistoryDialog',$aParent) ;
		//<<[UnmodifiableBlock.End]::Call Parent Construct::>>// 
		
		$this->aAccount = $aAccount ;
		
		$this->Initialize() ;
		
		
	}
	
	/**
	 * 初始化 视图 
	 *
	 * @access	protected
	 * @return	void
	 */
	protected function Initialize()
	{
		$this->Create() ;
		
		// 载入UI控件
		$this->AddUICtrl(new DateList($this)) ;
		$this->AddUICtrl(new UserList($this)) ;
		$this->AddUICtrl(new MessageHistory($this)) ;
		
		
		
		$this->LoadUserList() ;
	}

	/**
	 * 显示（或隐藏）视图
	 *
	 * @access	public
	 * @param	$bShow=true					bool	What's this Parameter ?
	 * @param	$bForAllChildren=false			bool	What's this Parameter ?
	 * @return	void
	 */
	public function ShowView($bShow=true,$bForAllChildren=false)
	{
		$this->LoadUserList();		
		parent::ShowView($bShow,$bForAllChildren) ;
	}
	
	/**
	 * 装载 用户 列表
	 *
	 * @access	public
	 * @return	void
	 */
	public function LoadUserList()
	{
		$theUserList = $this->GetUICtrl('UserList') ;
		
		// 清除原有
		$theUserList->RemoveAllItem() ;
		
		// 路径
		$sTopPath = ROOT_PATH.'Data/History/'.$this->aAccount->GetJabberAddress().'/' ;
		if( !is_dir($sTopPath) )
		{
			mkdir($sTopPath,0777,1) ;
		}
		
		// 历遍
		$aIterator = new JCAT_FSOIterator($sTopPath,JCAT_FSOIterator::DIR|JCAT_FSOIterator::RETURN_NAME) ;
		$aIterator->First() ;
		while( !$aIterator->IsDone() )
		{
			$sUserID = $aIterator->Current() ;
			$aContact = JJ_Account::CreateFromString($sUserID) ;
			if( $aContact )
			{
				$theUserList->AppendItem(array($aContact->GetUsername(),$aContact->GetJabberAddress())) ;
			}
			
			$aIterator->Next() ;
		}
	}
	/**
	 * 装载 日期 列表
	 *
	 * @access	public
	 * @return	void
	 */
	public function LoadDateList(JJ_Account $aAccount)
	{
		$theDateList = $this->GetUICtrl('DateList') ;
		
		// 清除原有
		$theDateList->RemoveAllItem() ;
		
		// 判断 记录是否存在
		$sUserDir = ROOT_PATH."Data/History/".$this->aAccount->GetJabberAddress().'/'.$aAccount->GetJabberAddress().'/' ;
		if( !is_dir($sUserDir) )
		{
			return ;
		}
		
		// 历遍
		$aIterator = new JCAT_FSOIterator($sUserDir,JCAT_FSOIterator::FILE|JCAT_FSOIterator::RETURN_NAME) ;
		$aIterator->First() ;
		while( !$aIterator->IsDone() )
		{
			$sDate = $aIterator->Current() ;
			
			$theDateList->AppendItem(array($sDate)) ;
			
			$aIterator->Next() ;
		}
	}

	/**
	 * 加载会话记录
	 *
	 * @access	public
	 * @return	void
	 */
	public function LoadSessionHistory(JJ_Account $aAccount,$sDate)
	{
		$sFile = ROOT_PATH."Data/History/".$this->aAccount->GetJabberAddress().'/'.$aAccount->GetJabberAddress().'/'.$sDate ;
		$sContent = file_get_contents($sFile) ;
		
		$aMessageHistory = $this->GetUICtrl('MessageHistory') ;
		$aMessageHistory->SetDataByExchange($sContent) ;
		
		// 移动到最下面
		$wnHistoryScrolled = $this->GetChildWidget('HistoryScrolled') ;
		$wndVAdjustment = $wnHistoryScrolled->get_vadjustment() ;
		$wndVAdjustment->set_value( $wndVAdjustment->upper - $wndVAdjustment->page_size ) ;
	}
	
	/**
	 * 选择 用户
	 *
	 * @access	public
	 * @return	void
	 */
	public function OnSelectUser(GtkTreeView $wndUserList)
	{$wndUserList ;
		$theUserList = $this->GetUICtrl('UserList') ;
		
		$aItem = $theUserList->GetFirstSelectedItem() ;
		if(!$aItem)
		{
			return ;
		}
		
		$sJID = $theUserList->GetCellData($aItem,UserList::CLM_USERID) ;
		$this->LoadDateList(JJ_Account::CreateFromString($sJID)) ;
	}
	
	/**
	 * 选择 用户
	 *
	 * @access	public
	 * @return	void
	 */
	public function OnSelectDate(GtkTreeView $wndUserList)
	{$wndUserList;
		$theUserList = $this->GetUICtrl('UserList') ;
		$theDateList = $this->GetUICtrl('DateList') ;
	
		$aUserItem = $theUserList->GetFirstSelectedItem() ;
		if(!$aUserItem)
		{
			return ;
		}
		$aDateItem = $theDateList->GetFirstSelectedItem() ;
		if(!$aDateItem)
		{
			return ;
		}

		$sContact = $theUserList->GetCellData($aUserItem,UserList::CLM_USERID) ;
		$sDate = $theDateList->GetCellData($aDateItem,DateList::CLM_DATE) ;

		$this->LoadSessionHistory(JJ_Account::CreateFromString($sContact),$sDate) ;
	}
	
	
	 /**
	  * 添加聊天记录
	  *
	  * @access	public
	  * @param	$sTo
	  * @param	$sAuthor
	  * @param	$sMessage
	  * @return	void
	  */
	 public function Append(JJ_Account $aAccount,$sMessageText)
	 {
	 	$aHistoryFile = $this->GetMessageFile($aAccount) ;
	 	$aHistoryFile->Write($sMessageText) ;
	 }
	 
	 /**
	  * 取得 文件 
	  *
	  * @access	private
	  * @param	$sTo
	  * @param	$sDate
	  * @return	JCAT_File
	  */
	 private function GetMessageFile(JJ_Account $aAccount,$sDate=null)
	 {
	 	if( $sDate===null )
	 	{
	 		$sDate = date('Y.m.d') ;
	 	}
	 	
	 	$sContact = $aAccount->GetJabberAddress() ;
	 	
	 	// 创建对象
	 	if(!isset($this->arrMessageHistoryFile[$sContact][$sDate]))
	 	{
			$sUserDir = ROOT_PATH."Data/History/".$this->aAccount->GetJabberAddress()."/{$sContact}/" ;
	 		if( !is_dir($sUserDir) )
	 		{
	 			mkdir($sUserDir,0777,1) ;
	 		}
	 		
	 		$sFilePath = "{$sUserDir}/{$sDate}" ;
	 		$this->arrMessageHistoryFile[$sContact][$sDate] = new JCAT_File($sFilePath,'a') ;
	 	}
	 	
	 	return $this->arrMessageHistoryFile[$sContact][$sDate] ;
	 }
	 
	 
	 /**
	  * 关闭
	  *
	  * @access	public
	  * @return	void
	  */
	 public function OnClose()
	 {
	 	$this->ShowView(false) ;
	 	return true ;
	 }

	 private $arrMessageHistoryFile = array() ;
	 
	 /**
	  * Description
	  * 
	  * @access	private
	  * @var		JJ_Account
	  */
	 private $aAccount ;
}

?><?
/**
 * [注意!!]
 *	以下内容由 JCAT PHP Toolbox 自动维护，手动更改的内容会被 JCAT PHP Toolbox 抹除，
 *	同时还可能破坏格式，导致 JCAT PHP Toolbox 无法正常读取。
 */
/*<<[UnmodifiableBlock.Start]::Coder Dialog Data::>>&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&//
<<{{DocumentClass:ViewGtkDoc}}>>
eCVEQSU5RFQlNUQlOTMlREE2JTE0JTdEJUVGTCU3RkR4byVDMCVDNm4lMTMlOTMlQzklMEMlRjhjYyUyNTIlMkIlNUIlMTIlQzMlQkV0JUJDJUEwJTAxYyU4MSVBOXElMDJmJTg3JUZGJURFJTIzJUMzbiUyNyVFOSVCNiVEMyVFOSVDMyUxREklRDdXJUU3JURFcyVFRSU5NXMlQ0YlRjYlOUUlMEUlOUUlRTMlRjUlODIl
QkMlQzklN0IlQTMlREMlRkIlQ0QlOUMlQUQlQTElRDclOTMlODUlM0ElRkElM0ElM0YlMUMlOTIlN0MlQUJ6JTIzOG0lQUZHJUZDMSVGRiVERCU3QyVCOWslQ0ElQ0UlODclOUIlMjIlRTZqJUJCJUQ3eS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